package com.sphd.education.adapter;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sphd.education.R;
import com.sphd.education.activitys.LessonMyEvaluationActivity;
import com.sphd.education.info.LessonInfo;
import com.sphd.education.utils.ToastUtils;

import java.io.Serializable;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by admin on 2015/10/22.
 */
public class LessonAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<LessonInfo> lessonInfoList;
    private int layoutInt;

    public LessonAdapter(Context context, List<LessonInfo> lessonInfoList, int layoutInt) {
        this.context = context;
        this.layoutInt = layoutInt;
        this.lessonInfoList = lessonInfoList;

    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(parent.getContext())
                .inflate(layoutInt, parent, false);
        final ViewHolder viewHold = new ViewHolder(view);
        return viewHold;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ((ViewHolder) holder).lessonSchoolName.setText(lessonInfoList.get(position).getSchoolName());
        ((ViewHolder) holder).lessonLessonName.setText("课程:\t" + lessonInfoList.get(position).getName());
        ((ViewHolder) holder).lessonClassName.setText("班级:\t" + lessonInfoList.get(position).getBanji());
        ((ViewHolder) holder).lessonLessonTime.setText("课时:\t" + lessonInfoList.get(position).getPeriod());
        switch (lessonInfoList.get(position).getState()) {
            case 1:
                ((ViewHolder) holder).lessonLessonState.setText("状态:\t" + "未开课");
                break;
            case 2:
                ((ViewHolder) holder).lessonLessonState.setText("状态:\t" + "已开课");
                break;
            case 3:
                ((ViewHolder) holder).lessonLessonState.setText("状态:\t" + "已结业");
                break;
            default:
                ((ViewHolder) holder).lessonLessonState.setText("状态:\t" + "解析错误");
                break;
        }
        ((ViewHolder) holder).lessonMyEvaluation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, LessonMyEvaluationActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("listLesson", lessonInfoList.get(position));
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });

    }

    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return lessonInfoList.size();
    }

    public void onClick(View v) {
        ToastUtils.MyToast(context, v.getTag() + "");
//        int a = (int) v.getTag();
//        Intent intent = new Intent(context, LessonMyEvaluationActivity.class);
//        Bundle bundle = new Bundle();
//        bundle.putString("SchoolName", lessonInfoList.get(a).getSchoolName());
//        bundle.putString("Name", lessonInfoList.get(a).getName());
//        bundle.putString("Banji", lessonInfoList.get(a).getBanji() + "");
//        bundle.putString("LessonTime", lessonInfoList.get(a).getPeriod());
////        bundle.putString("LessonPart", lessonInfoList.get(a).getLessonPart());//暂时不用显示
//        bundle.putString("State", lessonInfoList.get(a).getState() + "");
//        intent.putExtras(bundle);
//        context.startActivity(intent);
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.lesson_school_name)
        TextView lessonSchoolName;
        @Bind(R.id.lesson_my_evaluation)
        TextView lessonMyEvaluation;
        @Bind(R.id.lesson_lesson_name)
        TextView lessonLessonName;
        @Bind(R.id.lesson_class_name)
        TextView lessonClassName;
        @Bind(R.id.lesson_lesson_time)
        TextView lessonLessonTime;
        @Bind(R.id.lesson_lesson_state)
        TextView lessonLessonState;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }

    }
}
